package com.wjc.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjc.wiki.entity.Category;
import com.wjc.wiki.entity.CategoryExample;
import com.wjc.wiki.mapper.CategoryMapper;
import com.wjc.wiki.req.CategoryQueryReq;
import com.wjc.wiki.req.CategorySaveReq;
import com.wjc.wiki.resp.CategoryQueryResp;
import com.wjc.wiki.resp.PageResp;
import com.wjc.wiki.util.CopyUtil;
import com.wjc.wiki.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryService {

    private static final Logger LOG= LoggerFactory.getLogger(CategoryService.class);

    @Autowired
    private CategoryMapper categoryMapper;

    @Resource
    private SnowFlake snowFlake;

    public PageResp<CategoryQueryResp> list(CategoryQueryReq req){

        CategoryExample categoryExample =new CategoryExample();
        CategoryExample.Criteria criteria=categoryExample.createCriteria();

        //将查询到的对象转换为categoryresp,复制category对象到categoryresp
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);

        PageInfo<Category> pageInfo=new PageInfo<>(categoryList);
        LOG.info("总行数:{}",pageInfo.getTotal());
        LOG.info("总页数:{}",pageInfo.getPages());

//        List<CategoryResp> respList =new ArrayList<>();
//        for (Category category : categoryList) {
////           CategoryResp categoryResp = new CategoryResp();
////           BeanUtils.copyProperties(category,categoryResp);//beanutil springboot自带,方便复制对象
//           //对象复制
//            CategoryResp categoryResp = CopyUtil.copy(category,CategoryResp.class);
//
//           respList.add(categoryResp);//将转换完成的对象添加到list中
//        }
        //列表复制
        List<CategoryQueryResp> categoryRespList =CopyUtil.copyList(categoryList, CategoryQueryResp.class);

        PageResp<CategoryQueryResp> pageResq=new PageResp();
        pageResq.setTotal(pageInfo.getTotal());
        pageResq.setList(categoryRespList);

        return pageResq;
    }

    //保存
    public void save(CategorySaveReq req){
        Category category =CopyUtil.copy(req,Category.class);
        if (!ObjectUtils.isEmpty(req.getId())){//动态sql,如果req的id不为空,更新 否则新增
            //新增
            categoryMapper.updateByPrimaryKey(category);
        }else{
            category.setId(snowFlake.nextId());
            categoryMapper.insert(category);
        }

    }

    public List<CategoryQueryResp> all(){

        CategoryExample categoryExample =new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        CategoryExample.Criteria criteria=categoryExample.createCriteria();
        //将查询到的对象转换为categoryresp,复制category对象到categoryresp
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        List<CategoryQueryResp> categoryRespList =CopyUtil.copyList(categoryList, CategoryQueryResp.class);
        return categoryRespList;
    }

    public void delete(Long id){
        categoryMapper.deleteByPrimaryKey(id);
    }
}
